package com.sunyzc.ebaotech.ybt.simulator.ccbnew;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 该Processor可以直接用于环境中的CCBNEW银行的交易，无需修改配置。但实际因为CCBNEW与其他银行的报文发送和解析方式不一样，<b>该Processor的代码写的比较丑</b> <br>
 * P53816141 - 保费试算(104) <br>
 * P53819113 - 核保(101) <br>
 * P53819152 - 新单缴费(102) <br>
 */
public class CCBNEWProcessor extends BasicCCBNEWProcessor {
	private static final Logger log = LoggerFactory.getLogger(CCBNEWProcessor.class);
	private final static String TRANS_ENDCODING = "UTF-8";
	private BufferedReader reader = null;

	@Override
	public byte[] generateHeadBytes(String transType, byte[] bodyContent) throws Exception {
		StringBuffer httpHeader = new StringBuffer();
		httpHeader.append("HTTP/1.1 200 OK\r\n");
		httpHeader.append("Host:15.128.1.115:39871\r\n");
		httpHeader.append("Server:BIP 1.0\r\n");
		httpHeader.append("Date:" + new Date() + "BIP 1.0\r\n");
		httpHeader.append("Content-Type:application/x-www-form-urlencoded\r\n");
		httpHeader.append("Content-Length:" + bodyContent.length);
		httpHeader.append("Connection: keep-alive\r\n\r\n");

		StringBuffer safeHeader = new StringBuffer();
		safeHeader.append("SEC_ERROR:000000000000\r\n");
		safeHeader.append("SEC_IS_MAC:1\r\n");
		safeHeader.append("SEC_IS_CONTEXT:1\r\n");
		safeHeader.append("SEC_IS_ENC:1\r\n");
		safeHeader.append("SEC_ID1:105005\r\n");// remote
		safeHeader.append("SEC_ID2:510047\r\n");// local
		safeHeader.append("SEC_LEN:\r\n\r\n");

		String requestHeadContent = httpHeader.toString() + safeHeader.toString();
		return requestHeadContent.getBytes(TRANS_ENDCODING);
	}

	@Override
	public String readHeadConetentStr(String transType, InputStream inputStream) throws IOException {
		String headContentStr = null;
		reader = new BufferedReader(new InputStreamReader(inputStream, TRANS_ENDCODING));
		StringBuffer httpHeader = readHeader(reader);
		StringBuffer safeHeader = readHeader(reader);
		headContentStr = httpHeader.toString() + safeHeader.toString();
		return headContentStr;
	}

	private StringBuffer readHeader(BufferedReader reader) throws IOException {
		StringBuffer headContent = new StringBuffer();
		String line = "";
		while ((line = reader.readLine()) != null) {
			headContent.append(line).append("\r\n");
			if (line.equals("")) // CCBNEW的header以空行分割和结尾
				break;
		}
		return headContent;
	}

	@Override
	public String readBodyConetentStr(String transType, InputStream inputStream, String headContentStr) throws Exception {
		String bodyContentStr = null;
		StringBuffer bodyContent = new StringBuffer();
		int readChar = 0;
		while ((readChar = reader.read()) != -1) {
			if ((char) readChar == '\0')
				break;
			bodyContent.append((char) readChar);
		}
		bodyContentStr = bodyContent.toString();
		log.debug("交易[" + transType + "]响应未处理的报文体: " + bodyContentStr);
		transXmlMap.put("RP" + transType, bodyContentStr);
		return bodyContentStr;
	}

	@Override
	protected byte[] beforeSend(String bodyContentStr) throws Exception {
		return (bodyContentStr + "\0").getBytes(TRANS_ENDCODING); // 银保通前置机会以"\0"为报文结束符
	}

	@Override
	public String getTransEncoding() {
		return TRANS_ENDCODING;
	}
}
